package com.czd.support.controller;

import com.czd.support.model.Friendlink;
import com.czd.support.model.Manager;
import com.czd.support.service.FriendlinkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * Author: zhangrui
 * Create: zhangrui (2014/11/25)
 * Description:
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/friendlink")
public class FriendlinkController extends BaseController {

    @Autowired
    private FriendlinkService friendlinkService;

    /**
     * 友情链接实体列表页
     * @param model
     * @param friendlink
     * @return
     */
    @RequestMapping(value = {"","/","list"},method = RequestMethod.GET)
    public String list(Model model,Friendlink friendlink,@RequestParam(value = "pageNo",defaultValue = "1") int pageNo){
    	friendlink.setPageNo(pageNo);
        model.addAttribute("paginate",friendlinkService.queryForPaginate(friendlink));
        return "friendlink/list";
    }

    /**
     * 友情链接实体拦截
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "friendlink/add";
    }
    
    /**
     * 友情链接实体
     * @param friendlink
     * @param request
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(Friendlink friendlink,HttpServletRequest request,Model model){
        Manager manager = this.getSesionUser(request);
        friendlink.setCreateTime(manager.getCreateTime());
        friendlink.setCreateUser(manager.getCreateUser());
        if(friendlinkService.insert(friendlink)){
        	return "redirect:/friendlink/list";
        }
        model.addAttribute("msg","创建失败");
        return "friendlink/add";
    }
    
    /**
     * 友情链接实体编辑 拦截
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}/edit",method = RequestMethod.GET)
    public String view(@PathVariable int id,Model model){
    	Friendlink friendlink = friendlinkService.findById(id);
        if(friendlink != null){
            model.addAttribute("friendlink",friendlink);
            return "friendlink/edit";
        }
        return error(model,"操作错误","友情链接实体不存在");
    }
    
    /**
     * 友情链接实体编辑
     * @param id
     * @param friendlink
     * @param request
     * @return
     */
    @RequestMapping(value = "/{id}/update",method = RequestMethod.POST)
    public String update(@PathVariable int id,Friendlink friendlink,HttpServletRequest request,Model model){
    	Friendlink friendlinkData = friendlinkService.findById(id);
        if(friendlinkData != null){
            Manager manager = this.getSesionUser(request);
            friendlink.setUpdateTime(manager.getUpdateTime());
            friendlink.setUpdateUser(manager.getUpdateUser());
            if(StringUtils.isBlank(friendlink.getTitle())){
                model.addAttribute("error","友情链接实体名称不允许为空");
                return "friendlink/edit";
            } else {
                if (friendlinkService.update(friendlink)) {
                        return "redirect:/friendlink/list";
                    } else {
                        return error(model, "操作失败", "更新友情链接实体信息失败");
                }
            }
        }else{
            return error(model, "操作失败", "此友情链接实体已经不存在");
        }
    }
    
    /**
     * 广告实体删除 
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}/del",method = RequestMethod.POST)
    public String del(@PathVariable int id,Model model){ 
        if(friendlinkService.deleteById(id)){
        	return "redirect:/friendlink/list";
        } else {
        	return error(model,"操作错误","友情链接实体删除失败");
        }
    }
}

